package com.spareparts.activity;

import com.spareparts.R;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends ActivityGroup 
{
	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView mTextTitle;
	private Context mContext;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		mContext = this;
		
		mTabHost = (TabHost) findViewById(R.id.tabhost);
		mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
		mTextTitle = (TextView) findViewById(R.id.textViewTitle);
		
		mTabHost.setup();
		mTabHost.setup(this.getLocalActivityManager());  
		
		setTabHost();
		
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
		{
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) 
			{
				switch (checkedId) 
				{
					case R.id.radioButtonNavigation:
						mTabHost.setCurrentTabByTag("navigation");
						mTextTitle.setText(R.string.navigations_navigation_des);
						break;
					case R.id.radioButtonSpare:
						mTabHost.setCurrentTabByTag("spare");
						mTextTitle.setText(R.string.navigations_spare_des);
						break;
					case R.id.radioButtonNetwork:
						mTabHost.setCurrentTabByTag("network");
						mTextTitle.setText(R.string.navigations_network_des);
						break;
					case R.id.radioButtonWarehouse:
						mTabHost.setCurrentTabByTag("warehouse");
						mTextTitle.setText(R.string.navigations_warehouse_des);
						break;
				}
			}
		});
	}
	
	/**
	 * 设置下方选项卡点击时的跳转的activity
	 */
	private void setTabHost()
	{
		TabHost.TabSpec spec;
        Intent intent;
       
        intent=new Intent().setClass(this, NavigationActivity.class);
        spec=mTabHost.newTabSpec("navigation").setIndicator("navigation").setContent(intent);
        mTabHost.addTab(spec);
        
        intent=new Intent().setClass(this,WarehouseCKActivity.class);
        spec=mTabHost.newTabSpec("spare").setIndicator("spare").setContent(intent);
        mTabHost.addTab(spec);
        
        intent=new Intent().setClass(this, WarehouseRKActivity.class);
        spec=mTabHost.newTabSpec("network").setIndicator("network").setContent(intent);
        mTabHost.addTab(spec);
        
        intent=new Intent().setClass(this, OtherActivity.class);
        spec=mTabHost.newTabSpec("warehouse").setIndicator("warehouse").setContent(intent);
        mTabHost.addTab(spec);
        mTabHost.setCurrentTab(0);
        mTextTitle.setText(R.string.navigations_navigation_des);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		if(keyCode == KeyEvent.KEYCODE_BACK) 
		{ 
			AppExit();
		}
		return super.onKeyDown(keyCode, event);
	}  
	
	/**
	 * 退出程序
	 */
    private void AppExit()
    {
    	if(!((Activity)mContext).isFinishing())
		{
	        new AlertDialog.Builder(mContext).setTitle( R.string.app_name ).setMessage(
	        		mContext.getResources().getString( R.string.app_exit )).setPositiveButton( R.string.ok,
	        new DialogInterface.OnClickListener() {
	            @Override
	            public void onClick(DialogInterface dialog, int which) {
	                 {
	             		System.exit(0);
	                }
	            }
	        }).setNegativeButton( R.string.cancel,
	        new DialogInterface.OnClickListener() {
	            @Override
	            public void onClick(DialogInterface dialog, int which) {
	
	            }
	        }).show();
		}
    }

}
